﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using System.Diagnostics;
using MusicMashup.Models;

namespace MusicMashup.Controllers
{   
    public class SongController : ApiController
    {
        private MusicRepository rep;

        public SongController()
        {
            rep = new MusicRepository();
        }

        // Get a single song based on id
        public Song Get(int id)
        {
            Song song;
            
            try
            {
                song = rep.GetSongById(id);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            if (song == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            // Success:
            return song;
        }

        // Update information about a single song:
        public HttpResponseMessage Put(int id, String title, String artist, String genre)
        {
            try
            {
                rep.UpdateSong(id, title, artist, genre);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw new HttpResponseException(ex.Message, HttpStatusCode.InternalServerError);
            }
            // Success:
            return new HttpResponseMessage(HttpStatusCode.OK);
        }
    }
}
